package com.jtjrenren.android.taxi.passenger.core.wallect.recharge;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiRechargList;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.core.adapter.ChargeHistoryAdapter;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.SpaceItemDecoration;
import com.library.weidget.AutoLoadRecyclerView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wdl on 2016/1/27.
 * 充值明细
 */
public class RechargeHistoryActivity extends BaseActivity {
    @BindView(R.id.wallect_recharge_history_refresh)
    SwipeRefreshLayout swp_refresh;
    @BindView(R.id.wallect_recharge_history_list)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.nocontent)
    LinearLayout ll_nocontent;
    @BindView(R.id.title)
    TextView tv_title;

    private User user;
    private ChargeHistoryAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.aty_user_recharge_history;
    }

    @Override
    public void init() {
        tv_title.setText("充值明细");
        user = AppManger.getInstance().user;

        InitView.initSwipeRefreshLayout(swp_refresh);
        InitView.initVritalReclyView(this, rcy_list);
        rcy_list.addItemDecoration(new SpaceItemDecoration(0, 0, 5, 0));

        adapter = new ChargeHistoryAdapter(this);
        rcy_list.setAdapter(adapter);

        setListeners();

        getChargeHistory();
    }

    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }


    private void setListeners() {
        swp_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getChargeHistory();
            }
        });
        rcy_list.setLoadMoreListener(new AutoLoadRecyclerView.LoadMoreListener() {
            @Override
            public void loadMore() {

            }
        });
    }

    /**
     * 获取充值的历史记录
     */
    private void getChargeHistory() {
        showWaittingDialog(null, true);
        ApiWorks.getChargeHistory(user.getWorkNo(),
                new ApiWorks.ResponseListener<ApiRechargList>() {
                    @Override
                    public void onResponse(ApiRechargList apiRechargList) {
                        dismissWaittingDialog();
                        if (swp_refresh.isRefreshing())
                            swp_refresh.setRefreshing(false);

                        if (apiRechargList != null && apiRechargList.getRechargeList() != null && apiRechargList.getRechargeList().size() > 0) {
                            ll_nocontent.setVisibility(View.GONE);
                            adapter.replace(apiRechargList.getRechargeList());
                        } else {
                            ll_nocontent.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }
}
